﻿using CIS.Common.Entitys;
using CIS.Server.Repository.ProcessProgramRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CIS.Server.Manager.ProcessProgram
{
    public class ProcessProgramManager : IProcessProgramManager
    {
        private readonly IPPIDMappingRepository _ppidMappingRepository;
        private readonly IProcessProgramRepository _processProgramRepository;
        private readonly IRecipeRepository _recipeRepository;

        public ProcessProgramManager(IPPIDMappingRepository pPIDMappingRepository,IProcessProgramRepository processProgramRepository,IRecipeRepository recipeRepository)
        {
            _ppidMappingRepository = pPIDMappingRepository;
            _processProgramRepository = processProgramRepository;
            _recipeRepository = recipeRepository;
        }

        #region ProcessProgram 相关
        public async Task<bool> AddProcessProgramAsync(ProcessProgramEntity entity)
        {
            return await _processProgramRepository.AddAsync(entity);
        }

        public Task<bool> DeleteProcessProgramAsync(ProcessProgramEntity ppidEntity)
        {
            return _processProgramRepository.DeleteAsync(ppidEntity);
        }
        public async Task<List<ProcessProgramEntity>> GetProcessProgramsAsync()
        {
            return await _processProgramRepository.GetAllAsync();
        }

        public async Task<ProcessProgramEntity> GetProcessProgramByPPIDAsync(string ppid)
        {
            return await _processProgramRepository.GetFirst(p => p.PPID == ppid);
        }


        #endregion

        #region PPIDMapping 相关
        public async Task<bool> AddPPIDMappingAsync(PPIDMappingEntity entity)
        {
            return await _ppidMappingRepository.AddAsync(entity);
        }
        public Task<PPIDMappingEntity> GetPPIDMappingBySourceIdAsync(string sourceId)
        {
            return null;
        }

        public async Task<List<PPIDMappingEntity>> GetPPIDMappingsAsync()
        {
            return await _ppidMappingRepository.GetAllAsync();
        }
        public async Task<List<PPIDMappingEntity>> GetPPIDMappingsByPPIDAsync(string ppid)
        {
            return await _ppidMappingRepository.GetManyAsync(p => p.PPID == ppid);
        }
        #endregion

        #region Recipe 相关
        public Task<bool> AddRecipeAsync(RecipeEntity entity)
        {
            throw new NotImplementedException();
        }

        public async Task<RecipeEntity> GetRecipeByRecipeNoAsync(int recipeNo)
        {
            return await _recipeRepository.GetFirst(c=>c.RecipeNo == recipeNo);
        }

        public async Task<List<RecipeEntity>> GetRecipesBySourceIdAsync(string sourceId)
        {
            return await _recipeRepository.GetManyAsync(c => c.SourceId.Contains(sourceId));
        }

        public async Task<List<RecipeEntity>> GetAllRecipesAsync()
        {
            return await _recipeRepository.GetAllAsync();
        }


        #endregion
    }
}
